package com.chen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.dao.mapper.AlbumMapper;
import com.chen.dao.mapper.CardMapper;
import com.chen.dao.mapper.SystemSettingMapper;
import com.chen.dao.pojo.Album;
import com.chen.dao.pojo.Card;
import com.chen.dao.pojo.SystemSetting;
import com.chen.service.SystemService;
import com.chen.util.FileUtils;
import com.chen.vo.AlbumVo;
import com.chen.vo.Result;

import com.chen.vo.param.AlbumParam;
import com.chen.vo.param.CardParam;
import com.chen.vo.param.CardVo;
import com.chen.vo.param.FirstPageParam;
import com.chen.vo.FirstPageVo;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Author:chenao
 * @DATE:2022/3/3 19:45
 */
@Service
public class SystemServiceImpl implements SystemService {

    @Autowired
    private SystemSettingMapper systemSettingMapper;
    @Autowired
    private FileUtils fileUtils;
    @Autowired
    private CardMapper cardMapper;
    @Autowired
    private AlbumMapper albumMapper;

    public Result addSwiperSetting(MultipartFile file, Map<String,String> params){
        Map<String,String> map = fileUtils.saveFile(file, "swiper");
        SystemSetting systemSetting = new SystemSetting();
        systemSetting.setImageUrl(map.get("imageUrl"));
        systemSetting.setFilePath(map.get("filePath"));
        systemSetting.setName(params.get("name"));
        systemSetting.setSettingName(params.get("settingName"));
        systemSetting.setEditTime(new Date().getTime());
        this.systemSettingMapper.insert(systemSetting);
        return Result.success("success");
    }

    public Result listSetting(FirstPageParam pageParam){
        Page<SystemSetting> page = new Page<>(pageParam.getPage(),pageParam.getLimit());
        LambdaQueryWrapper<SystemSetting> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(SystemSetting::getSettingName,pageParam.getSettingName());
        wrapper.like(SystemSetting::getName,pageParam.getName());
        systemSettingMapper.selectPage(page, wrapper);
        List<FirstPageVo> list = new LinkedList<>();
        page.getRecords().stream().forEach(item->{
            FirstPageVo firstPageVo = new FirstPageVo();
            firstPageVo.setId(item.getId());
            firstPageVo.setEditTime(item.getEditTime());
            firstPageVo.setName(item.getName());
            firstPageVo.setSettingName(item.getSettingName());
            firstPageVo.setImageUrl(item.getImageUrl());
            firstPageVo.setEditUser("Martins");
            list.add(firstPageVo);
        });
        return Result.success(list,page.getTotal());
    }

    @Override
    public Result addCardSetting(MultipartFile file, Map<String, String> params) {
        Map<String,String> map = fileUtils.saveFile(file, "card");
        Card card = new Card();
        card.setImageUrl(map.get("imageUrl"));
        card.setFilePath(map.get("filePath"));
        card.setCategory(params.get("category"));
        card.setIntroduce(params.get("introduce"));
        card.setSales(0);
        card.setPrice(params.get("price"));
        //如果携带了id，代表修改操作
        if (Strings.isNotBlank(params.get("id"))){
            card.setId(params.get("id"));
            cardMapper.updateById(card);
            return Result.successMsg("修改成功");
        }
        cardMapper.insert(card);
        return Result.successMsg("添加成功");
    }

    @Override
    public Result listCard(CardParam param) {
        Page<Card> page = new Page<>(param.getPage(),param.getLimit());
        LambdaQueryWrapper<Card> wrapper = new LambdaQueryWrapper<>();
        if (Strings.isNotBlank(param.getCategory())){
            wrapper.like(Card::getCategory,param.getCategory());
        }
        cardMapper.selectPage(page, wrapper);
        List<CardVo> list = new LinkedList<>();
        page.getRecords().stream().forEach(item->{
            CardVo cardVo = new CardVo();
            BeanUtils.copyProperties(item,cardVo);
            list.add(cardVo);
        });
        return Result.success(list,page.getTotal());
    }

    @Override
    public Result deleteCardById(String id) {
        int result = cardMapper.deleteById(id);
        return Result.success(result);
    }

    @Override
    public Result deleteSettingById(String id) {
        int result = systemSettingMapper.deleteById(id);
        return Result.success(result);
    }

    @Override
    public Result addAlbum(MultipartFile file, Map<String, String> params) {
        Map<String,String> map = fileUtils.saveFile(file, "album");
        Album album = new Album();
        album.setImageUrl(map.get("imageUrl"));
        album.setFilePath(map.get("filePath"));
        album.setName(params.get("name"));
        album.setCategory(params.get("category"));
        if (Strings.isNotBlank(params.get("id"))){
            album.setId(params.get("id"));
            albumMapper.updateById(album);
            return Result.success("修改成功");
        }
        albumMapper.insert(album);
        return Result.success("添加成功");
    }

    @Override
    public Result listAlbum(AlbumParam param) {
        Page<Album> page = new Page<>(param.getPage(),param.getLimit());
        LambdaQueryWrapper<Album> wrapper = new LambdaQueryWrapper<>();
        if (Strings.isNotBlank(param.getCategory())){
            wrapper.like(Album::getCategory,param.getCategory());
        }
        albumMapper.selectPage(page, wrapper);
        List<AlbumVo> list = new LinkedList<>();
        page.getRecords().stream().forEach(item->{
            AlbumVo albumVo = new AlbumVo();
            BeanUtils.copyProperties(item,albumVo);
            list.add(albumVo);
        });
        return Result.success(list,page.getTotal());
    }

    @Override
    public Result deleteAlbumById(String id) {
        int i = albumMapper.deleteById(id);
        return Result.success(i);
    }
}
